﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using youtubeFisher;
using System.IO;
using System.Net;

namespace Coldflame.dotnet.libraries.online.google.youtube
{
    public class downloader
    {
        public bool _audio = false;
        public string dwnFile, title;
        public static string filepath;
        public WebClient wc;
        public YouTubeService yts = null;

        public string gettitle(string url)
        {
            string temptitle = correctcheckurl(url);
            if (temptitle != ".:?:?")
            {
                yts = YouTubeService.Create(url);
                return yts.VideoTitle;
                yts = null;
            }
            else
            {
                return temptitle;
            }

        }

        public string correctcheckurl(string url)
        {
            string uri = url.Trim();
            if (url.StartsWith("https://")) url = "http://" + url.Substring(8);
            else if (!url.StartsWith("http://")) url = "http://" + url;

            url = url.Replace("youtu.be/", "youtube.com/watch?v=");
            url = url.Replace("www.youtube.com", "youtube.com");

            if (url.StartsWith("http://youtube.com/v/"))
                url = url.Replace("youtube.com/v/", "youtube.com/watch?v=");
            else if (url.StartsWith("http://youtube.com/watch#"))
                url = url.Replace("youtube.com/watch#", "youtube.com/watch?");

            if (!url.StartsWith("http://youtube.com/watch"))
            {
                return ".:?:?";
            }
            else
            {
                return uri;
            }
        }

        public string getdownloadlink(string url)
        {
            string tempdlink = correctcheckurl(url);
            if (tempdlink != ".:?:?")
            {
                yts = YouTubeService.Create(url);
                return yts.VideoUrl;
                yts = null;
            }
            else
            {
                return tempdlink;
            }
        }
    }
}
